package controllers

import (
	"github.com/gin-gonic/gin"
	"go-admin-official/guard"
	"go-admin-official/models"
	"go-admin-official/modules/auth"
	"net/http"
)

type PluginPage struct {
	User    models.UsersModel        `json:"user"`
	List    []map[string]interface{} `json:"list"`
	Count   int                      `json:"count"`
	HasMore bool                     `json:"has_more"`
}

func Plugins(ctx *gin.Context) {
	user, _ := auth.Filter(ctx)
	param := guard.GetPluginsParam(ctx)
	list, count, hasMore := models.Plugin().List(param, user.Id, false)
	ctx.HTML(http.StatusOK, "plugins.tmpl", PluginPage{
		User:    user,
		List:    list,
		Count:   count,
		HasMore: hasMore,
	})
}

func ApiPlugins(ctx *gin.Context) {
	user, _ := auth.Filter(ctx)
	param := guard.GetPluginsParam(ctx)
	list, count, hasMore := models.Plugin().List(param, user.Id, true)
	ctx.JSON(http.StatusOK, gin.H{
		"code": 0,
		"data": gin.H{
			"list":     list,
			"count":    count,
			"has_more": hasMore,
			"page": gin.H{
				"css": `
.login-username {
	text-align: right;
    margin-bottom: 10px;
    margin-right: 2px;
	cursor: pointer;
}
`,
				"js": `
$(function(){
    let login_token = localStorage.getItem("GOADMIN_SERVER_API_TOKEN");
	if (login_token && login_token != "") {
		let expire_time = localStorage.getItem("GOADMIN_SERVER_API_TOKEN_EXPIRE");
		if (expire_time > (new Date()).getTime()) {
			$(".login-username").html("已登录：" + localStorage.getItem("GOADMIN_SERVER_LOGIN_NAME"));
		} else {
			localStorage.removeItem("GOADMIN_SERVER_API_TOKEN");
			localStorage.removeItem("GOADMIN_SERVER_API_TOKEN_EXPIRE");
		}
	} else {
		$(".login-username").on('click', function(){
			$('#login-popup-modal').modal('show');
		})
	}
});

function loginSuccessCallback(data) {
	localStorage.setItem("GOADMIN_SERVER_LOGIN_NAME", data.data.name);
	$(".login-username").html("已登录：" + data.data.name);
	$(".login-username").unbind('click');
}
`,
				"html": `
<div class="row">
	<div class="col-sm-12 col-md-12 col-lg-12">
		<div class="login-username">未登录(GoAdmin会员系统)</div>
	</div>
</div>`,
			},
		},
	})
}

type PluginDetailPage struct {
	User   models.UsersModel
	Plugin models.PluginModel
}

func PluginDetail(ctx *gin.Context) {
	user, _ := auth.Filter(ctx)
	uuid := ctx.Param("uuid")
	lang := ctx.Query("lang")
	if lang == "" {
		lang = "cn"
	}
	ctx.HTML(http.StatusOK, "plugin_detail.tmpl", PluginDetailPage{
		User:   user,
		Plugin: models.Plugin().FindByUUID(uuid, lang, user.Id),
	})
}

func PluginDownload(ctx *gin.Context) {
	user, _ := auth.Filter(ctx)
	param := guard.GetPluginsDownloadParam(ctx)
	plug := models.Plugin().FindByUUID(param.Uuid)
	if !models.UserBuyPlugins().CheckBuyOrNot(plug.Id, user.Id) {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "wrong request, has not bought",
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code": 0,
		"data": gin.H{
			"url":       plug.GetDownloadURL(),
			"extra_url": plug.GetExtraDownloadURL(),
		},
		"msg": "ok",
	})
}

type PluginsBuyPage struct {
	User    models.UsersModel
	List    []map[string]interface{}
	Count   int
	HasMore bool
}

func PluginsBuy(ctx *gin.Context) {
	user, _ := auth.Filter(ctx)
	param := guard.GetPluginsBuyParam(ctx)
	list, count, hasMore := models.UserBuyPlugins().List(param)
	ctx.HTML(http.StatusOK, "user_buy_plugin.tmpl", PluginsBuyPage{
		User:    user,
		List:    list,
		Count:   count,
		HasMore: hasMore,
	})
}

//<div class="tabs-wrapper panel panel-default p-2 hidden-xs">
//<div class="tabs-group">
//<div class="title">分类:</div>
//<ul class="list-unstyled list-inline">
//<li class="active"><a class="nav-link" href="?category=all&amp;pay=all&amp;order=default">全部</a></li>
//<li class=""><a class="nav-link" href="?category=19&amp;pay=all&amp;order=default">完整应用</a></li>
//<li class=""><a class="nav-link" href="?category=31&amp;pay=all&amp;order=default">小程序</a></li>
//<li class=""><a class="nav-link" href="?category=14&amp;pay=all&amp;order=default">开发测试</a></li>
//<li class=""><a class="nav-link" href="?category=16&amp;pay=all&amp;order=default">编辑器</a></li>
//<li class=""><a class="nav-link" href="?category=17&amp;pay=all&amp;order=default">云储存</a></li>
//<li class=""><a class="nav-link" href="?category=18&amp;pay=all&amp;order=default">短信验证</a></li>
//<li class=""><a class="nav-link" href="?category=15&amp;pay=all&amp;order=default">接口整合</a></li>
//<li class=""><a class="nav-link" href="?category=21&amp;pay=all&amp;order=default">辅助增强</a></li>
//<li class=""><a class="nav-link" href="?category=0&amp;pay=all&amp;order=default">未归类</a></li>
//</ul>
//</div>
//<div class="tabs-group">
//<div class="title">价格:</div>
//<ul class="list-unstyled list-inline">
//<li class="active"><a class="nav-link" href="?category=all&amp;pay=all&amp;order=default">全部</a></li>
//<li class=""><a class="nav-link" href="?category=all&amp;pay=price&amp;order=default">免费</a></li>
//<li class=""><a class="nav-link" href="?category=all&amp;pay=price&amp;order=default">付费</a></li>
//</ul>
//</div>
//<div class="tabs-group">
//<div class="title">排序:</div>
//<ul class="list-unstyled list-inline">
//<li class="active"><a class="nav-link" href="?category=all&amp;pay=all&amp;order=default">默认排序</a></li>
//<li class=""><a class="nav-link" href="?category=all&amp;pay=free&amp;order=default">按下载次数</a></li>
//<li class=""><a class="nav-link" href="?category=all&amp;pay=price&amp;order=default">按发布时间</a></li>
//<li class=""><a class="nav-link" href="?category=all&amp;pay=price&amp;order=default">按更新时间</a></li>
//</ul>
//</div>
//</div>
//
//
//.tabs-wrapper {
//border-radius: 2px;
//background-clip: padding-box;
//margin-bottom: 30px;
//background-color: #fff;
//padding: 10px 0;
//}
//.panel-default {
//padding: 0 15px;
//border: none;
//}
//.p-2 {
//padding-top: 10px !important;
//padding-right: 10px !important;
//padding-bottom: 10px !important;
//padding-left: 10px !important;
//}
//.tabs-wrapper .tabs-group {
//padding: 0;
//position: relative;
//overflow-y: hidden;
//}
//.tabs-wrapper .tabs-group .title {
//float: left;
//padding: 15px 0;
//width: 40px;
//display: block;
//overflow: hidden;
//text-overflow: ellipsis;
//white-space: nowrap;
//word-wrap: normal;
//text-align: right;
//font-size: 14px;
//}
//.tabs-wrapper .tabs-group ul {
//list-style: none;
//padding: 0;
//margin: 0 0 0 60px;
//}
//.tabs-wrapper .tabs-group ul > li {
//float: left;
//padding: 10px 12px;
//}
//.tabs-wrapper .tabs-group ul > li.active > a, .tabs-wrapper .tabs-group ul > li:focus > a, .tabs-wrapper .tabs-group ul > li:hover > a {
//color: #43bc60;
//}
//.tabs-wrapper .tabs-group ul > li > a {
//display: block;
//padding: 5px 10px;
//border: none;
//border-radius: 4px;
//background-clip: padding-box;
//color: #919191;
//font-size: 14px;
//}
//.addon-filter {
//position: relative;
//min-height: 48px;
//margin: 0 0 10px 0;
//padding-right: 150px;
//}
//.addon-filter .nav-pills > li {
//margin: 0 30px 10px 0;
//}
//.addon-filter .nav-pills > li {
//margin: 0 30px 10px 0;
//}
